// Copyright 2010 Google Inc. All Rights Reserved.

package com.google.zahra.webmanager;

import com.google.zahra.testmanager.TestManager;
import com.google.zahra.util.Util;
import com.google.zahra.workmanager.ZahraWorkManager;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author arthurkok@google.com (Your Name Here)
 *
 */

public class AllTestServlet extends HttpServlet {

  static final ZahraWorkManager workManager = WebManager.WORK_MANAGER;
  static final TestManager testManager = WebManager.TEST_MANAGER;

  @SuppressWarnings("unchecked")
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);

    String clearFlag = request.getParameter("clear");
    int page =
        request.getParameter("page") == null ? 0 : Integer.parseInt(request.getParameter("page"));
    StringBuffer bodyString = new StringBuffer();

    if (clearFlag == null) {
      Vector<Vector<Object>> data = testManager.model.getDataVector();

      // All completed tests
      bodyString.append("<h2>Completed Tests</h2>");

      bodyString.append("<table class='clear'><tr><td>");
      if (page != 0) {
        bodyString.append("<a href='/allTest?page=" + 0 + "'><< First</a>  ");
        bodyString.append("<a href='/allTest?page=" + (page - 1) + "'><< Previous</a><br/>");
      }

      bodyString.append("</td><td style='text-align:right'>");

      if ((page + 1) * 50 < data.size()) {
        bodyString.append("<a href='/allTest?page=" + (page + 1) + "'>Next >></a>  ");
        bodyString.append(
            "<a href='/allTest?page=" + (data.size() - 1) / 50 + "'>Last >></a><br/>");
      }
      bodyString.append("</td></tr></table><br/>");

      bodyString.append("<table>");
      bodyString.append(
          String.format("<table border='1'><tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>",
              "Job ID", "Windows", "Mac", "Unix"));

      for (int x = page * 50; x < page * 50 + 50; x++) {
        if (x >= data.size()) {
          break;
        }
        Vector<Object> row = data.get(x);

        String output =
            "<tr><td><a href='/viewTest?jobId=" + row.elementAt(0) + "'>" + row.elementAt(0)
                + "</a></td>";

        for (int col = 1; col < 4; col++) {
          if (row.elementAt(col) == null) {
            output += "<td>No Test Scheduled</td>";
          } else {
            output += "<td>" + row.elementAt(col) + "</td>";
          }

        }

        output += "</tr>";
        bodyString.append(output);

      }

      bodyString.append("</table><a name='bottom'/>");
    } else if (clearFlag.equals("table")) {
      testManager.clearTable();
      bodyString.append("Cleared the Table<br/>");
    } else if (clearFlag.equals("pending")) {
      try {
        workManager.clear();
        bodyString.append("Cleared the pending queues");
      } catch (InterruptedException e) {
        Thread.interrupted();
        e.printStackTrace();
        bodyString.append("Error clearing pending queues");
      }
    }
    response.getWriter().print(Util.getHTML(bodyString.toString(), "Zahra - All Tests"));
  }
}
